package appbuilder.wizards.gui.data.model.validator;

import appbuilder.wizards.gui.data.model.DatabaseSettings;

import com.jgoodies.validation.ValidationResult;
import com.jgoodies.validation.Validator;
import com.jgoodies.validation.util.PropertyValidationSupport;
import com.jgoodies.validation.util.ValidationUtils;

public class DatabaseSettingsValidator implements Validator<DatabaseSettings> {
	private static final String BLANK                        = "";
	private static final String MANDATORY_URL                = "mandatory.url";
	private static final String MANDATORY_DRIVER_PATH        = "mandatory.driverPath";
	private static final String MANDATORY_DRIVER             = "mandatory.driver";
	private static final String VALIDATION_DATABASE_SETTINGS = "validation.databaseSettings";

	@Override
	public ValidationResult validate(DatabaseSettings settings) {
		PropertyValidationSupport support = new PropertyValidationSupport(settings, VALIDATION_DATABASE_SETTINGS);
		
		if (ValidationUtils.isBlank(settings.getDriver())) {
			support.addError(MANDATORY_DRIVER, BLANK);
		}

		if (ValidationUtils.isBlank(settings.getPath())) {
			support.addError(MANDATORY_DRIVER_PATH, BLANK);
		}
		
		if (ValidationUtils.isBlank(settings.getUrl())) {
			support.addError(MANDATORY_URL, BLANK);
		}
		
		return support.getResult();
	}
}
